<?php
    class NaturalExtensionAjax_Bookshelf {
        private $utils,
                $book_page_id,
                $header_menu_id,
                $book_page_default_titles;

        public function __construct() {
            $this->utils = new NaturalExtension_Utils();
            $menu_locations = get_theme_mod('nav_menu_locations');

            $this->book_page_id = $this->utils->get_page_id_via_name('book');
            $this->header_menu_id = $menu_locations['header-menu'];
            $this->book_page_default_titles = array(
                'About_the_Book' => 'About the Books',
                'Book'           => 'Books'
            );
        }

        public function get_book_data() {
            $post_id        = $this->getData($_POST['post_id'], '0');
            $book           = get_post($post_id);
            $post_thumb_url = has_post_thumbnail($post_id) ? wp_get_attachment_url(get_post_thumbnail_id($post_id)) : OF_DIRECTORY . '/patch/images/dummy-book-cover.jpg';

            exit(json_encode(array(
                'post_id'               => $post_id,
                'post_thumbnail_url'    => $post_thumb_url,
                'post_thumbnail_id'     => get_post_thumbnail_id($post_id),
                'post_title'            => $book->post_title,
                'post_content'          => apply_filters('the_content', $book->post_content),
                'bookstore_link'        => get_post_meta($post_id, 'bookstore_link', true),
                'excerpt_home'          => get_post_meta($post_id, 'excerpt_home', true),
                'excerpt_book'          => get_post_meta($post_id, 'excerpt_book', true),
                'excerpt_pages'         => get_post_meta($post_id, 'excerpt_pages', true)
            )));
        }

        public function add_book() {
            $post_id = $this->getData($_POST['post_id'], '');
            $mode = ($post_id == '') ? 'add' : 'edit';

            // prepare post data
            $post_args = array(
                'post_type'         => 'page',
                'post_status'       => 'publish',
                'ID'                => $post_id,
                'post_parent'       => $this->book_page_id,
                'post_title'        => $this->getData($_POST['post_title'], 'Title'),
                'post_content'      => $this->getData($_POST['post_content'], '')
            );

            // add or update book
            $book_id = wp_insert_post($post_args);
            $book_menu_id = $this->utils->get_page_menu_id($book_id);

            update_post_meta($book_id, "_wp_page_template", 'page-right-sidebar.php');
            update_post_meta($book_id, "_thumbnail_id", $this->getData($_POST['featured_image_id'], ''));
            update_post_meta($book_id, "bookstore_link", $this->getData($_POST['bookstore_link'], ''));
            update_post_meta($book_id, "excerpt_home", $this->getData($_POST['excerpt_home'], ''));
            update_post_meta($book_id, "excerpt_book", $this->getData($_POST['excerpt_book'], ''));
            update_post_meta($book_id, "excerpt_pages", $this->getData($_POST['excerpt_pages'], ''));
            update_post_meta($book_id, "bookstore_auto_content", $this->getData($_POST['bookstore_auto_content'], ''));


            $books = $this->utils->get_bookshelf_items();

            $this->update_submenu_item($post_args['post_title'], $book_id, $book_menu_id);

            // pluralize the parent page's title only if it matches the default title
            // because we don't want to override custom titles requested by the author
            if (sizeof($books) > 1) {
                $this->update_parent_title('pluralize');
            }


            exit();
        }

        public function get_bookshelf_items() {
            $bookshelf = new NaturalExtension_Bookshelf();

            $bookshelf->display_books();

            exit();
        }

        public function remove_bookshelf_item() {
            $post_id = $this->getData($_POST['post_id'], '');

            wp_delete_post($post_id);
            $this->remove_submenu_item($post_id);

            $books = $this->utils->get_bookshelf_items();

            // singularize the parent page's title
            if (sizeof($books) == 1) {
                $this->update_parent_title('singularize');
            }

            exit();
        }

        public function add_ajax_action($action, $callback) {
            add_action('wp_ajax_nopriv_' . $action, array($this, $callback));
            add_action('wp_ajax_' . $action, array($this, $callback));
        }

        public function getData($data, $noData = '') {
            return empty($data) ? $noData : $data;
        }

        public function update_submenu_item($menu_title, $page_id, $menu_id) {
            $book_page_menu_id = $this->utils->get_page_menu_id($this->book_page_id);

            wp_update_nav_menu_item($this->header_menu_id, $menu_id, array(
                'menu-item-title'       => $menu_title,
                'menu-item-parent-id'   => $book_page_menu_id,
                'menu-item-position'    => $page_id,
                'menu-item-object'      => 'page',
                'menu-item-object-id'   => $page_id,
                'menu-item-url'         => get_page_link($page_id),
                'menu-item-type'        => 'post_type',
                'menu-item-status'      => 'publish'
            ));
        }

        public function remove_submenu_item($page_id) {
            $menu_item_id = $this->utils->get_page_menu_id($page_id);

            wp_delete_post($menu_item_id, true);
        }

        public function pluralize($title) {
            if (in_array($title, array_keys($this->book_page_default_titles))) {
                return $this->book_page_default_titles[$title];
            }

            return $title;
        }

        public function singularize($title) {
            if (in_array($title, array_values($this->book_page_default_titles))) {
                return array_search($title, $this->book_page_default_titles);
            }

            return $title;
        }

        public function update_parent_title($transform) {
            $parent_page = get_post($this->book_page_id);
            $parent_page_args = array();
            $parent_page_args['ID'] = $this->book_page_id;
            $parent_page_args['post_title'] = $this->$transform($parent_page->post_title);

            wp_update_post($parent_page_args);
        }
    }

    $natext_ajax_bookshelf = new NaturalExtensionAjax_Bookshelf();

    $natext_ajax_bookshelf->add_ajax_action('NatExtAjax_Bookshelf', 'get_book_data');
    $natext_ajax_bookshelf->add_ajax_action('NatExtAjax_Bookshelf_AddBook', 'add_book');
    $natext_ajax_bookshelf->add_ajax_action('NatExtAjax_Bookshelf_GetItems', 'get_bookshelf_items');
    $natext_ajax_bookshelf->add_ajax_action('NatExtAjax_Bookshelf_RemoveItems', 'remove_bookshelf_item');
?>